package com.dwxt.sport.business.service;

import com.dwxt.common.base.StatusCode;
import com.dwxt.common.constant.VariableConstants;
import com.dwxt.common.util.JsonUtils;
import com.dwxt.common.util.SnowIdUtil;
import com.dwxt.sport.business.dao.*;
import com.dwxt.sport.business.entity.StreetDeviceClassify;
import com.dwxt.sport.business.view.DistributionMapView;
import com.dwxt.sport.entity.Scene;
import com.dwxt.sport.entity.WifeDevice;
import com.dwxt.sport.exception.CustomException;
import com.dwxt.sport.service.SceneService;
import com.dwxt.sport.service.WifeDeviceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 街道 & 设备分类 Service
 *
 * @author Kuang.JiaZhuo
 * @date 2020-08-11 15:00
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class StreetDeviceClassifyService {

    private final StreetDeviceClassifyMapper streetDeviceClassifyMapper;
    private final StreetMapper streetMapper;
    private final WifeDeviceService wifeDeviceService;
    private final SceneService sceneService;
    private final OutdoorFitnessMapper outdoorFitnessMapper;
    private final FitnessSurveyMapper fitnessSurveyMapper;
    private final SmartDeviceMapper smartDeviceMapper;
    private final AdolescentSportsHealthMapper adolescentSportsHealthMapper;
    private final QuinquagenarianSportsHealthMapper quinquagenarianSportsHealthMapper;
    private final DeformitySportsHealthMapper deformitySportsHealthMapper;
    private final SportsTrainingMapper sportsTrainingMapper;
    private final WisdomTrailsMapper wisdomTrailsMapper;
    private final WisdomVenuesMapper wisdomVenuesMapper;


    /**
     * @方法名：insertOrUpdateStreetDeviceClassify
     * @描述： 新增 街道&设备分类
     * @作者： kjz
     * @日期： Created in 2020/8/11 15:16
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateStreetDeviceClassify(StreetDeviceClassify streetDeviceClassify) {
        String id = streetDeviceClassify.getId();
        if (null == id || id.trim().length() == 0) {
            //新增
            streetDeviceClassify.setId(SnowIdUtil.getInstance().nextId())
                    .setCreateTime(LocalDateTime.now())
                    .setIsDelete(VariableConstants.STRING_CONSTANT_0);
            return streetDeviceClassifyMapper.insert(streetDeviceClassify);
        } else {
            //更新
            streetDeviceClassify.setUpdateTime(LocalDateTime.now());
            return streetDeviceClassifyMapper.updateByPrimaryKeySelective(streetDeviceClassify);
        }
    }

    /**
     * @方法名：selectStreetDeviceClassifyByClassification
     * @描述： 按设备分类返回 【街道&设备分类】
     * @作者： kjz
     * @日期： Created in 2020/8/11 15:29
     */
    public PageInfo<StreetDeviceClassify> selectStreetDeviceClassifyByClassification(String classification, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum==null?1:pageNum, pageSize==null?10:pageSize);
        List<StreetDeviceClassify> list = streetDeviceClassifyMapper.selectStreetDeviceClassifyByClassification(classification);
        list.forEach(streetDeviceClassify -> {
            String streetId = streetDeviceClassify.getStreetId();
            String streetName = streetMapper.selectByPrimaryKey(streetId).getName();
            streetDeviceClassify.setName(streetName + streetDeviceClassify.getClassificationDisplay());
        });
        PageInfo<StreetDeviceClassify> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * @方法名：deletetStreetDeviceClassifyByIds
     * @描述： 根据ids批量删除【街道&设备分类】
     * @作者： kjz
     * @日期： Created in 2020/8/11 15:51
     */
    @Transactional(rollbackFor = Exception.class)
    public int deletetStreetDeviceClassifyByIds(String ids) {

        if (ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        String[] idArray = ids.split(",");

        return this.streetDeviceClassifyMapper.deletetStreetDeviceClassifyByIds(idArray);
    }

    /**
     * @方法名：selectDistributionMapViewByClassification
     * @描述： 根据设备分类查询[首页-智能物联分布]地图
     * @作者： kjz
     * @日期： Created in 2020/8/12 17:04
     */
    public List selectDistributionMapViewByClassification(String[] classifications) {

        List list = new ArrayList<>();
//        System.err.println(classifications == null);
//        System.err.println(classifications.length==0);
        if (classifications == null || classifications.length==0) {
//            list = streetDeviceClassifyMapper.selectDistributionMapViewByAll();
//            System.err.println(list);
            return list; //20200819最新修改，不传参数就返回空的list
        } else {
            List<String> classificationList = Arrays.asList(classifications);

            if (null == classificationList || classificationList.size() == 0) {
                list = streetDeviceClassifyMapper.selectDistributionMapViewByAll();
            } else {
                //视频监控
                if (classificationList.contains(VariableConstants.STRING_CONSTANT_1)) {
                    List<WifeDevice> wifeDeviceList = wifeDeviceService.selectByType(2);
                    List<Scene>scenes=
                            sceneService.selectByDeviceIdList(wifeDeviceList.stream().map(WifeDevice::getId).collect(Collectors.toList()));
                    List<Object> classification = scenes.stream().map(scene -> {

                        Map map = JsonUtils.jsonToPojo(JsonUtils.objectToJson(scene), Map.class);
                        map.put("classification",
                                        VariableConstants.STRING_CONSTANT_1);
                        return map;
                            }).collect(Collectors.toList());
                    list.addAll(classification);
                }
                //wifi探针
                if (classificationList.contains(VariableConstants.STRING_CONSTANT_3)) {
                    List<WifeDevice> wifeDeviceList = wifeDeviceService.selectByType(1);
                    List<Scene>scenes=
                            sceneService.selectByDeviceIdList(wifeDeviceList.stream().map(WifeDevice::getId).collect(Collectors.toList()));
                    List<Object> classification = scenes.stream().map(scene -> {
                        Map map = JsonUtils.jsonToPojo(JsonUtils.objectToJson(scene), Map.class);
                        map.put("classification",
                                VariableConstants.STRING_CONSTANT_3);
                        return map;
                    }).collect(Collectors.toList());
                    list.addAll(classification);
                }
                //室外健身器材
                if(classificationList.contains(VariableConstants.STRING_CONSTANT_2)){
                    List<DistributionMapView> distributionMapViews = outdoorFitnessMapper.selectDistributionMapViewByAll();
                    distributionMapViews.forEach(distributionMapView -> distributionMapView.setClassification(VariableConstants.STRING_CONSTANT_2));
                    list.addAll(distributionMapViews);
                }
                //体质监测中心
                if(classificationList.contains(VariableConstants.STRING_CONSTANT_4)){
                    List<DistributionMapView> distributionMapViews = fitnessSurveyMapper.selectDistributionMapViewByAll();
                    distributionMapViews.forEach(distributionMapView -> distributionMapView.setClassification(VariableConstants.STRING_CONSTANT_4));
                    list.addAll(distributionMapViews);
                }
                //智能装备
                if(classificationList.contains(VariableConstants.STRING_CONSTANT_5)){
                    List<DistributionMapView> distributionMapViews = smartDeviceMapper.selectDistributionMapViewByAll();
                    distributionMapViews.forEach(distributionMapView -> distributionMapView.setClassification(VariableConstants.STRING_CONSTANT_5));
                    list.addAll(distributionMapViews);
                }
                //青少年运动健康
                if(classificationList.contains(VariableConstants.STRING_CONSTANT_6)){
                    List<DistributionMapView> distributionMapViews = adolescentSportsHealthMapper.selectDistributionMapViewByAll();
                    distributionMapViews.forEach(distributionMapView -> distributionMapView.setClassification(VariableConstants.STRING_CONSTANT_6));
                    list.addAll(distributionMapViews);
                }
                //中老年运动健康
                if(classificationList.contains(VariableConstants.STRING_CONSTANT_7)){
                    List<DistributionMapView> distributionMapViews = quinquagenarianSportsHealthMapper.selectDistributionMapViewByAll();
                    distributionMapViews.forEach(distributionMapView -> distributionMapView.setClassification(VariableConstants.STRING_CONSTANT_7));
                    list.addAll(distributionMapViews);

                }
                //残疾人运动健康
                if(classificationList.contains(VariableConstants.STRING_CONSTANT_8)){
                    List<DistributionMapView> distributionMapViews = deformitySportsHealthMapper.selectDistributionMapViewByAll();
                    distributionMapViews.forEach(distributionMapView -> distributionMapView.setClassification(VariableConstants.STRING_CONSTANT_8));
                    list.addAll(distributionMapViews);
                }
                //体育培训
                if(classificationList.contains(VariableConstants.STRING_CONSTANT_9)){
                    List<DistributionMapView> distributionMapViews = sportsTrainingMapper.selectDistributionMapViewByAll();
                    distributionMapViews.forEach(distributionMapView -> distributionMapView.setClassification(VariableConstants.STRING_CONSTANT_9));
                    list.addAll(distributionMapViews);
                }
                //智慧步道
                if(classificationList.contains(VariableConstants.STRING_CONSTANT_10)){
                    List<DistributionMapView> distributionMapViews = wisdomTrailsMapper.selectDistributionMapViewByAll();
                    distributionMapViews.forEach(distributionMapView -> distributionMapView.setClassification(VariableConstants.STRING_CONSTANT_10));
                    list.addAll(distributionMapViews);
                }
                //智慧场馆
                if(classificationList.contains(VariableConstants.STRING_CONSTANT_11)){
                    List<DistributionMapView> distributionMapViews = wisdomVenuesMapper.selectDistributionMapViewByAll();
                    distributionMapViews.forEach(distributionMapView -> distributionMapView.setClassification(VariableConstants.STRING_CONSTANT_11));
                    list.addAll(distributionMapViews);
                }
                System.err.println("classificationList="+classificationList);
            }
        }
        return list;
    }
}
